<?php
$host="db.seng.uvic.ca"; // Host name
$username="se321g01"; // Mysql username
$password="un8bi2YM"; // Mysql password
$db_name="se321g01"; // Database name
$tbl_name="rewards"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


// Get UID
$rid = $_GET['rid'];

// Get post variables
$title = $_POST['title'];
$description = $_POST['description'];
$cost = $_POST['cost'];
$file = $_FILES["filepicker"]["name"];

echo $title . " ";
echo $description . " ";
echo $cost . " ";
echo $file . "<br/>";

// Add to rewards table
mysql_query("INSERT INTO rewards (title, description, points, path) VALUES
('$title', '$description', '$cost', '$file')");

//Add file to proper location on server
  if ($_FILES["filepicker"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["filepicker"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["filepicker"]["name"] . "<br />";
    echo "Type: " . $_FILES["filepicker"]["type"] . "<br />";
    echo "Size: " . ($_FILES["filepicker"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["filepicker"]["tmp_name"] . "<br />";

    if (file_exists("/home/se321g01/www/prototype/images/landing/rewards/" . $_FILES["filepicker"]["name"]))
      {
      echo $_FILES["filepicker"]["name"] . " already exists. ";
      }
    else
      {
      	if (file_exists($_FILES["filepicker"]["tmp_name"]))
      	{
      		$moved = move_uploaded_file($_FILES["filepicker"]["tmp_name"],
      		"/home/se321g01/www/prototype/images/landing/rewards/" . $_FILES["filepicker"]["name"]);
      		
      		if($moved)
      		{
      			echo "Stored in: " . "/home/se321g01/www/prototype/images/landing/rewards/" . $_FILES["filepicker"]["name"];
      		}
			else
			{
				echo "Move failed";
			}
      	}
      }
    }

// Go back to profile
header("location:./profile.php?uid=" . $uid . "&tab=3");

?>
